import { apiClient } from '../index';

// 资金费率订单列表参数接口
export interface FundingOrderListParams {
  page: number;
  page_size: number;
  execution_mode: string;
}

// 资金费率订单项接口
export interface FundingOrder {
  order_id: string;
  position_id: string;
  symbol: string;
  exchange: string;
  side: string;
  order_type: string;
  type: string;
  price: number;
  amount: number;
  filled: number;
  remaining: number;
  average_price: number;
  total_value: number;
  status: string;
  fee: number;
  pnl: number;
  execution_mode: string;
  created_at: string;
  executed_at: string;
  error_message: string | null;
}

// 资金费率订单列表响应接口
export interface FundingOrderListResponse {
  success: boolean;
  data: {
    total: number;
    page: number;
    page_size: number;
    total_pages: number;
    orders: FundingOrder[];
  };
}

// 跨交易所对冲订单列表参数接口
export interface SpreadOrderListParams {
  page: number;
  page_size: number;
  is_simulation: boolean;
}

// 跨交易所对冲订单项接口
export interface SpreadOrder {
  record_id: string;
  symbol: string;
  long_exchange: string;
  short_exchange: string;
  long_price: number;
  short_price: number;
  long_amount: number;
  short_amount: number;
  position_size: number;
  price_spread_pct: number;
  total_pnl: number;
  funding_income: number;
  expected_profit: number;
  status: string;
  entry_time: string;
  exit_time: string;
  holding_duration: string;
  risk_level: string;
  execution_mode: string;
  is_simulation: boolean;
  close_reason: string;
  long_order_id: string;
  short_order_id: string;
}

// 跨交易所对冲订单列表响应接口
export interface SpreadOrderListResponse {
  success: boolean;
  data: {
    total: number;
    page: number;
    page_size: number;
    total_pages: number;
    orders: SpreadOrder[];
  };
}

// 获取资金费率订单列表
export const getFundingOrderList = async (params: FundingOrderListParams): Promise<FundingOrderListResponse> => {
  try {
    const response = await apiClient.post<FundingOrderListResponse>('/orders/funding/list', params);
    return response.data;
  } catch (error) {
    console.error('获取资金费率订单列表失败:', error);
    throw error;
  }
};

// 获取跨交易所对冲订单列表
export const getSpreadOrderList = async (params: SpreadOrderListParams): Promise<SpreadOrderListResponse> => {
  try {
    const response = await apiClient.post<SpreadOrderListResponse>('/orders/spread/list', params);
    return response.data;
  } catch (error) {
    console.error('获取跨交易所对冲订单列表失败:', error);
    throw error;
  }
};